#pragma once
#include "cumacros.h"

template <typename T>
inline void cudebug_shm (char const *key, T *A, int ndims, ...)
{
	va_list ap;
	shm_array_t A_size, *A_shm;
	A_size.type = shm_array_type_map<T>::type;


	va_start (ap, ndims);
	int N = 1;
	A_size.ndims = ndims;	
	for (int i = 0; i < ndims; ++i){
		A_size.dims[i] = va_arg (ap, int);

		N = N * A_size.dims[i];
	}

	T *p = new T[N];

	cuda(Memcpy(p,A,N*sizeof(T),cudaMemcpyDeviceToHost));

	make_shm_array (key, &A_size, &A_shm);

	memcpy (A_shm->data.c, p, sizeof(*A)*N);

	put_shm_array  (key, A_shm);

	std::cerr << "Wrote shm_array(\"" << key << "\", " << N << " " 
		      << shm_type_strings[A_size.type] << "s)" << std::endl;

	delete [] p;
}
